package com.lidong.lowcode.model.service.impl;

import com.lidong.config.domain.gloabreturn.ResponseInfo;
import com.lidong.lowcode.base.domain.BaseParam;
import com.lidong.lowcode.base.domain.TreeVO;
import com.lidong.lowcode.base.domain.baseconst.BaseConst;
import com.lidong.lowcode.base.domain.baseconst.TreeConst;
import com.lidong.lowcode.model.domain.BaseGroup;
import com.lidong.lowcode.model.domain.BaseGroupExample;
import com.lidong.lowcode.model.mapper.BaseGroupMapper;
import com.lidong.lowcode.model.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 5/10/2022 8:05 PM
 */
@Service
public class ModelServiceImpl implements ModelService {


    @Autowired
    private BaseGroupMapper groupMapper;

    /**
     * 获取数据源分组树
     *
     * @param baseParam
     * @return ResponseInfo
     */
    @Override
    public ResponseInfo<List<TreeVO>> getTree(BaseParam baseParam) {
        Map<String, Object> extMap = baseParam.getExtInfo();
        List<BaseGroup> groups = Optional.ofNullable(groupMapper.selectByExample(new BaseGroupExample())).orElse(Collections.emptyList());
        String type = null == extMap.get(BaseConst.KEY_TYPE) ? "" : extMap.get(BaseConst.KEY_TYPE).toString();
        if (!StringUtils.isEmpty(type)) {
            groups = groups.stream().filter(x -> type.equalsIgnoreCase(x.getType())).collect(Collectors.toList());
        }
        List<TreeVO> trees = new ArrayList<>();
        List<BaseGroup> rootNodes = groups.stream().filter(x -> TreeConst.ROOT_CODE.equals(x.getParentId())).collect(Collectors.toList());
        for (BaseGroup rootNode : rootNodes) {
            TreeVO treeVO = new TreeVO();
            treeVO.setId(rootNode.getId());
            treeVO.setName(rootNode.getGroupName());
            treeVO.setCode(rootNode.getGroupCode());
            treeVO.setParentId(rootNode.getParentId());
            List<TreeVO> children = new ArrayList<>();
            this.dealChildren(treeVO.getId(), children, groups);
            treeVO.setChildren(children);
            trees.add(treeVO);
        }
        return ResponseInfo.success(trees);
    }

    /**
     * 递归处理树分组
     *
     * @param id       nodeId
     * @param children children
     * @param groups   groups
     */
    private void dealChildren(String id, List<TreeVO> children, List<BaseGroup> groups) {
        List<BaseGroup> childNodes = groups.stream().filter(x -> x.getParentId().equals(id)).collect(Collectors.toList());
        for (BaseGroup childNode : childNodes) {
            TreeVO tempTreeVO = new TreeVO();
            tempTreeVO.setId(childNode.getId());
            tempTreeVO.setName(childNode.getGroupName());
            tempTreeVO.setCode(childNode.getGroupCode());
            tempTreeVO.setParentId(childNode.getParentId());
            List<TreeVO> tempChildren = new ArrayList<>();
            this.dealChildren(tempTreeVO.getId(), tempChildren, groups);
            tempTreeVO.setChildren(tempChildren);
            children.add(tempTreeVO);
        }
    }
}
